package com.cckat.controller;

import com.cckat.base.BaseController;
import com.cckat.entity.Community;
import com.cckat.entity.Dict;
import com.cckat.service.CommunityService;
import com.cckat.service.DictService;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;


/**
 * @author 静海基地
 */
@Controller
@RequestMapping("/community")
public class CommunityController extends BaseController {

    @DubboReference
    private DictService dictService;
    @DubboReference
    private CommunityService communityService;

    @RequestMapping
    public String findPage(Model model, @RequestParam Map<String, Object> filters) {
        //获取北京所有的区域信息
        List<Dict> areaList = dictService.findDictListByParentDictCode("beijing");
        //查询小区的分页信息
        PageInfo<Community> page = communityService.findPage(filters);
        //将相关信息共享到请求域
        model.addAttribute("areaList", areaList);
        if (filters.size() == 0) {
            filters.put("areaId", "");
            filters.put("plateId", "");
        }
        model.addAttribute("filters", filters);
        model.addAttribute("page", page);
        return "community/index";
    }

    @RequestMapping("/create")
    public String create(Model model) {
        //获取北京所有的区域信息
        List<Dict> areaList = dictService.findDictListByParentDictCode("beijing");
        //将相关信息共享到请求域
        model.addAttribute("areaList", areaList);
        return "community/create";
    }

    @RequestMapping("/save")
    public String insert(Model model, Community community) {
        communityService.insert(community);
        return successPage("新增小区成功", model);
    }


    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, Model model) {
        //根据id查询要修改的小区信息
        Community community = communityService.getById(id);
        //将小区相关信息共享到请求域回显
        model.addAttribute("community", community);

        //获取北京所有的区域信息，用于修改界面选择框选择区域
        List<Dict> areaList = dictService.findDictListByParentDictCode("beijing");
        //将所获取的信息共享到请求域用于显示在界面
        model.addAttribute("areaList", areaList);
        //跳转到修改界面
        return "community/edit";
    }

    @RequestMapping("/update")
    public String update(Community community, Model model) {
        //执行更新操作
        communityService.update(community);
        //跳转到操作成功界面并显示提示信息
        return successPage("修改操作成功", model);
    }


    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id){
        communityService.delete(id);
        return "redirect:/community";
    }
}
